package com.tc.repositories;

import com.tc.pojo.Customer;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Auther: tianchao
 * @Date: 2021/12/4 01:22
 * @Description:
 */
public interface CustomerSpecificationsRepository extends
        PagingAndSortingRepository<Customer,Long>,
        JpaSpecificationExecutor<Customer> {


    @Query(" FROM Customer where custName=?1 ")
    Customer findCustomerByCustName(String custName);

    @Query(" FROM Customer where custName=:custName ")
    Customer find2CustomerByCustName(@Param("custName") String custName);
    @Transactional
    @Modifying //通知springdatajpa 是增删改操作
    @Query(" UPDATE Customer c set c.custName =:custName where c.custId = :id")
    int updateCustomer(@Param("custName") String custName, @Param("id") Long id);

    @Transactional
    @Modifying //通知springdatajpa 是增删改操作
    @Query(" DELETE FROM Customer c WHERE c.custId = ?1")
    int deleteCustomer(Long id);
    @Transactional
    @Modifying //通知springdatajpa 是增删改操作
    @Query(" INSERT INTO Customer (custName) select c.custName from Customer c where c.custId = ?1")
    int insertCustomerBySelect(Long id);

    @Query(nativeQuery = true,value = " select * from tb_customer where cust_name = :custName")
    List<Customer> findCustomerByCustNameByNative(@Param("custName") String custName);
}
